package com.menhai.personmanagement.services;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.menhai.personmanagement.entity.JbfxReport;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.Codelist;
import com.menhai.personmanagement.entity.MbReportCard;
import com.menhai.personmanagement.entity.co.MbReportCardCO;
import com.menhai.personmanagement.entity.vo.MbReportCardVo;
import com.menhai.personmanagement.mapper.CodelistMapper;
import com.menhai.personmanagement.mapper.FollowupRecordMapper;
import com.menhai.personmanagement.mapper.MbPopulationMapper;
import com.menhai.personmanagement.mapper.ReportCardMapper;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
/**
 * <p>
 * 报卡管理表 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */
 @Service
public class ReportCardService{

   @Autowired
   private FollowupRecordMapper followupRecordMapper;
   @Autowired
   private ReportCardMapper  reportCardMapper;
   @Autowired
   private CodelistMapper codelistMapper;
   @Autowired
   private MbPopulationMapper mbPopulationMapper;



	/**
	 * @describe 患者列表
	 * @param mbReportCardCO
	 * @Return com.baomidou.mybatisplus.core.metadata.IPage<com.menhai.personmanagement.entity.MbReportCard>
	 * @Date 2021/12/14 15:40
	 * @author shikun
	 */
		public IPage<MbReportCardVo> selectByList(MbReportCardCO mbReportCardCO){
			Page<MbReportCardVo> page = new Page<>(mbReportCardCO.getPageIndex(),mbReportCardCO.getPageSize());
			return reportCardMapper.selectByList(page,mbReportCardCO);
		}

	/**
	 * 新增慢病报卡
	 * @param mbReportCard
	 * @return
	 */
  	@Transactional
  	public Map<String, Object> addMbReportCard(MbReportCard mbReportCard) {
  		Map<String,Object> resultMap = new HashMap<String,Object>();
  		try{
  			int iRet;
  			//同一个人同一慢病类型的报卡只能建一张
  			int selectIsSame = reportCardMapper.selectIsSame(mbReportCard.getPopulationid(), mbReportCard.getMbType());
  			if(selectIsSame>0){
  				resultMap.put("success", "0");
  				resultMap.put("message", "同一患者，慢病类型不能相同！");
  				return resultMap;
  			}
  			//查询是否有新建记录
  			List<Map<String, Object>> oneById = reportCardMapper.getOneById(mbReportCard.getBkbh());
  			if(oneById!=null && oneById.size()>0){//更新
  				//更新慢病
  				iRet = reportCardMapper.update(mbReportCard);
  			}else{//插入
  				//新建慢病报卡
  	  			iRet = reportCardMapper.insert(mbReportCard);
  			}
  			if(iRet == 0){  //没有插入成功的数据
  				resultMap.put("success", "0");
  				resultMap.put("message", "插入成功的条数为0，操作慢病报卡失败！");
  				return resultMap;
  			}else{
  				resultMap.put("success", "1");
  				resultMap.put("message", "操作慢病报卡成功！");
  				return resultMap;
  			}
  		}catch(Exception e){
  			resultMap.put("success", "0");
  			resultMap.put("message", "操作慢病报卡失败！异常：" +e.getMessage());
  			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  		}
  		return resultMap;
  	}


  	/**
	 * 删除报卡(更新状态)
	 * @param
	 * @return Map<String, Object>
	 */
	@Transactional
	public Map<String, Object> updateIsDelete(String bkbh) {
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			//删除随访记录信息
			int iRet = reportCardMapper.updateIsDelete(bkbh);
			if(iRet == 0){
				resultMap.put("success", "0");
				resultMap.put("message", "删除成功的条数为0，删除报卡失败！");
				return resultMap;
			}
			resultMap.put("success", "1");
			resultMap.put("message", "删除报卡成功！");
		}catch(Exception e){
			resultMap.put("success", "0");
			resultMap.put("message", "删除报卡失败！异常：" +e.getMessage());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return resultMap;
	}

	/**
	 * 根据条件查询
	 * @param patientName
	 * @param shstatus
	 * @param sbstatus
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<MbReportCard> getListByCondition(String patientName,String shstatus,String sbstatus,String startDate,String endDate){
		return reportCardMapper.getListByCondition(patientName, shstatus, sbstatus, startDate, endDate,null);
	}

	/**
	 * @describe 每日慢病数
	 * @param superiorCode
	 * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
	 * @Date 2022/1/24 9:54
	 * @author shikun
	 */
	public List<MbReportCardVo> selectBysuperiorCodeAndGroup(String superiorCode){
		return reportCardMapper.selectBysuperiorCodeAndGroup(superiorCode);
	}

	/**
	 * @describe 慢病患者年龄统计
	 * @param superiorCode
	 * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
	 * @Date 2022/1/24 10:35
	 * @author shikun
	 */
	public List<MbReportCardVo> selectPatientAgeCombination(String superiorCode){
		return reportCardMapper.selectPatientAgeCombination(superiorCode);
	}

	/**
	 * @describe 慢病患者病症统计
	 * @param superiorCode
	 * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
	 * @Date 2022/1/24 10:48
	 * @author shikun
	 */
	public List<MbReportCardVo> selectByGroupMbType(String superiorCode){
		return reportCardMapper.selectByGroupMbType(superiorCode);
	}


	public MbReportCardVo sicknumberstatistics(String superiorCode){

		QueryWrapper<MbReportCard> filterquery = new QueryWrapper<>();
		filterquery.likeRight("hosnum",superiorCode);
		int total =reportCardMapper.selectCount(filterquery);//总人数

		//高血压
		QueryWrapper<MbReportCard> hypertension = new QueryWrapper<>();
		hypertension.eq("mbType","高血压");
		hypertension.likeRight("hosnum",superiorCode);
		String hypertensionnumber  = String.valueOf(reportCardMapper.selectCount(hypertension));

		QueryWrapper<MbReportCard> tumor = new QueryWrapper<>();
		tumor.eq("mbType","肿瘤");
		tumor.likeRight("hosnum",superiorCode);
		String tumornumber  = String.valueOf(reportCardMapper.selectCount(tumor));

		QueryWrapper<MbReportCard> coronaryheart = new QueryWrapper<>();
		coronaryheart.eq("mbType","冠心病");
		coronaryheart.likeRight("hosnum",superiorCode);
		String coronaryheartnumber  = String.valueOf(reportCardMapper.selectCount(coronaryheart));

		QueryWrapper<MbReportCard> diabetes = new QueryWrapper<>();
		diabetes.eq("mbType","糖尿病");
		diabetes.likeRight("hosnum",superiorCode);
		String diabetesnumber  = String.valueOf(reportCardMapper.selectCount(diabetes));


		QueryWrapper<MbReportCard> mentalillness = new QueryWrapper<>();
		mentalillness.eq("mbType","精神病");
		mentalillness.likeRight("hosnum",superiorCode);
		String mentalillnessnumber  = String.valueOf(reportCardMapper.selectCount(mentalillness));


		QueryWrapper<MbReportCard> other = new QueryWrapper<>();
		other.ne("mbType","高血压");
		other.ne("mbType","肿瘤");
		other.ne("mbType","冠心病");
		other.ne("mbType","糖尿病");
		other.ne("mbType","精神病");
		other.likeRight("hosnum",superiorCode);
		String othernumber  = String.valueOf(reportCardMapper.selectCount(other));

		MbReportCardVo mbReportCardVo = new MbReportCardVo();
		mbReportCardVo.setTotal(String.valueOf(total));
		mbReportCardVo.setHypertension(hypertensionnumber);
		mbReportCardVo.setTumor(tumornumber);
		mbReportCardVo.setCoronaryheart(coronaryheartnumber);
		mbReportCardVo.setDiabetes(diabetesnumber);
		mbReportCardVo.setMentalillness(mentalillnessnumber);
		mbReportCardVo.setOther(othernumber);
		return mbReportCardVo;
	}

	/**
	 * @describe  慢病患者病症统计 前五
	 * @param superiorCode
	 * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
	 * @Date 2022/1/24 11:14
	 * @author shikun
	 */
	public List<MbReportCardVo> selectByGroupMbTypeLimltOrder(String superiorCode){
		return reportCardMapper.selectByGroupMbTypeLimltOrder(superiorCode);
	}

	public IPage<MbReportCard> getListByConditionByPage(MbReportCard mbreportCard,String minAge,String maxAge,String startDate,String endDate,List hosnumarray,String hosnumstr){
        Page<MbReportCard> page = new Page<>(mbreportCard.getPageIndex(),mbreportCard.getPageSize());
        return reportCardMapper.getListByConditionByPage(page,mbreportCard,minAge,maxAge,startDate,endDate,hosnumarray,hosnumstr);
    }

	/**
	 * 患者健康档案查询
	 * @param mbreportCard
	 * @return
	 */
	public IPage<Map<String,Object>> getMbHealthFile(MbReportCard mbreportCard){
        Page<Map<String,Object>> page = new Page<>(mbreportCard.getPageIndex(),mbreportCard.getPageSize());
        return reportCardMapper.getMbHealthFile(page,mbreportCard);
    }


	/**
	 * 获取字典表行政区划(省)
	 * @return
	 */
	public List<Codelist> selectProvince(){
		return codelistMapper.selectProvince();
	}


	 /**
	  * 获取字典表行政区划(市)
	  * @return
	  */
	 public List<Codelist> selectCity(String option1,String option2){
		 return codelistMapper.selectCity(option1, option2);
	 }


	 /**
	  * 上报
	  * @param sbTime
	  * @param bkbh
	  * @return
	  */
	 @Transactional
	 public Map<String,Object> updateSbstatus(String sbTime,String bkbh){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
			try{
				//查询是否审核,未审核不允许上报
				Map<String, Object> sbStatus = reportCardMapper.getShStatus(bkbh);
				if(sbStatus!=null && sbStatus.size()>0){
					if(!"2".equals(sbStatus.get("shstatus").toString())){
						Integer iRet = reportCardMapper.updateSbstatus(sbTime, bkbh);
						if(iRet == 0){
							resultMap.put("success", "0");
							resultMap.put("message", "上报成功的条数为0，上报报卡失败！");
							return resultMap;
						}
						resultMap.put("success", "1");
						resultMap.put("message", "上报报卡成功！");
					}else{
						resultMap.put("success", "0");
						resultMap.put("message", "请先审核！");
					}
				}else{
					resultMap.put("success", "0");
					resultMap.put("message", "上报报卡失败!未查询到数据");
				}
			}catch(Exception e){
				resultMap.put("success", "0");
				resultMap.put("message", "上报报卡失败！异常：" +e.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			return resultMap;
	 }


	 /**
	  * 取消上报
	  * @param bkbh
	  * @return
	  */
	 @Transactional
	 public Map<String,Object> deleteSbstatus(String bkbh){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
			try{
				Integer iRet = reportCardMapper.deleteSbstatus(bkbh);
				if(iRet == 0){
					resultMap.put("success", "0");
					resultMap.put("message", "取消上报成功的条数为0，取消上报报卡失败！");
					return resultMap;
				}
				resultMap.put("success", "1");
				resultMap.put("message", "取消上报报卡成功！");
			}catch(Exception e){
				resultMap.put("success", "0");
				resultMap.put("message", "取消上报报卡失败！异常：" +e.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			return resultMap;
	 }

	 /**
	  * 审核
	  * @param shTime
	  * @param bkbh
	  * @return
	  */
	 @Transactional
	 public Map<String,Object> updateShstatus(String shTime,String bkbh){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
			try{
				Integer iRet = reportCardMapper.updateShstatus(shTime, bkbh);
				if(iRet == 0){
					resultMap.put("success", "0");
					resultMap.put("message", "审核成功的条数为0，审核报卡失败！");
					return resultMap;
				}
				resultMap.put("success", "1");
				resultMap.put("message", "审核报卡成功！");
			}catch(Exception e){
				resultMap.put("success", "0");
				resultMap.put("message", "审核报卡失败！异常：" +e.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			return resultMap;
	 }


	/**
	 * 取消 审核
	 * @param bkbh
	 * @return
	 */
	@Transactional
	 public Map<String,Object> deleteShstatus(String bkbh){
		 Map<String,Object> resultMap = new HashMap<String,Object>();
			try{
				Integer iRet = reportCardMapper.deleteShstatus(bkbh);
				if(iRet == 0){
					resultMap.put("success", "0");
					resultMap.put("message", "取消审核成功的条数为0，取消审核报卡失败！");
					return resultMap;
				}
				resultMap.put("success", "1");
				resultMap.put("message", "取消审核报卡成功！");
			}catch(Exception e){
				resultMap.put("success", "0");
				resultMap.put("message", "取消审核报卡失败！异常：" +e.getMessage());
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
			return resultMap;
	 }

	 /**
	  * 根据报卡编号查询数据
	  * @param bkbh
	  * @return
	  */
	 public Map<String,Object> getOneById(String bkbh){
		 return reportCardMapper.getOneById(bkbh)==null?null:reportCardMapper.getOneById(bkbh).get(0);
	 }


	/**
	 * 编辑慢病报卡
	 * @param mbReportCard
	 * @return
	 */
	@Transactional
  	public Map<String, Object> updateMbReportCard(MbReportCard mbReportCard) {
  		Map<String,Object> resultMap = new HashMap<String,Object>();
  		try{
  			int iRet;
  			//同一个人同一慢病类型的报卡只能建一张
  			int selectIsSame = reportCardMapper.selectIsSame(mbReportCard.getPopulationid(), mbReportCard.getMbType());
  			if(selectIsSame>0){
  				resultMap.put("success", "0");
  				resultMap.put("message", "该村民已经建过报卡");
  				return resultMap;
  			}
  		    //更新慢病报卡
			iRet = reportCardMapper.update(mbReportCard);
  			if(iRet == 0){  //没有插入成功的数据
  				resultMap.put("success", "0");
  				resultMap.put("message", "插入成功的条数为0，操作慢病报卡失败！");
  				return resultMap;
  			}else{
  				resultMap.put("success", "1");
  				resultMap.put("message", "操作慢病报卡成功！");
  				return resultMap;
  			}
  		}catch(Exception e){
  			resultMap.put("success", "0");
  			resultMap.put("message", "操作慢病报卡失败！异常：" +e.getMessage());
  			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  		}
  		return resultMap;
  	}

  	/**
  	 * 获取镇列表
  	 * @param hosnum
  	 * @return
  	 */
  	public List<Codelist> getZhenList(@Param("hosnum") String hosnum){
  		return codelistMapper.getZhenList(hosnum);
  	}

  	/**
  	 * 获取村列表
  	 * @param hosnum
  	 * @return
  	 */
  	public List<Codelist> getCunList(@Param("hosnum") String hosnum){
  		return codelistMapper.getCunList(hosnum);
  	}

  	/**
  	 * 慢病新建报卡  查询人口姓名集合
  	 * @param patientName
  	 * @param hosnum
  	 * @param mblx
  	 * @return
  	 */
  	public List<Map<String,Object>> getReportPatient(String patientName,String hosnum,String mblx){
  		return reportCardMapper.getReportPatient(patientName, hosnum, mblx);
  	}

  	/**
  	 * 据当前系统时间，重新计算年龄
  	 * @param bkbh
  	 * @return
  	 */
  	public Integer updateAge(String bkbh){
  		return reportCardMapper.updateAge(bkbh);
  	}

  	/**
  	 * 规范人员管理统计表
  	 * @return
  	 */
//  	public List<Map<String,Object>> chronicPatientGroupsStatistics(String hosnum,String year){
//  		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
//  		//所有村名
//  		List<Codelist> chronicPatientGroupsStatisticsCun = codelistMapper.chronicPatientGroupsStatisticsCun(hosnum);
//  		for (Codelist codelist : chronicPatientGroupsStatisticsCun) {
//  			Map<String,Object> map= new HashMap<String,Object>();
//  			map.put("cunName", codelist.getCodeName());
//  			//每个村的总人口数
//  			int chronicPatientGroupsStatisticsTotal = mbPopulationMapper.chronicPatientGroupsStatisticsTotal(year, codelist.getCodeValue());
//  			map.put("cunTotal", chronicPatientGroupsStatisticsTotal);
//  			//确诊人数
//  			int chronicPatientGroupsStatisticsQZTotal = mbPopulationMapper.chronicPatientGroupsStatisticsQZTotal(year, codelist.getCodeValue());
//  			map.put("cunQZTotal", chronicPatientGroupsStatisticsQZTotal);
//  			//高血压登记人数
//  			int chronicPatientGroupsStatisticsResgister = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"高血压");
//  	  		map.put("gxydjTotal", chronicPatientGroupsStatisticsResgister);
//  			//高血压规范管理人数(年随访>=5)
//  	  		int chronicPatientGroupsStatisticsGFGL = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("高血压", codelist.getCodeValue(), year);
//  		    map.put("gxygfglTotal", chronicPatientGroupsStatisticsGFGL);
//  	  		//糖尿病登记人数
//  		    int chronicPatientGroupsStatisticsResgister2 = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"糖尿病");
//	  		map.put("tnbdjTotal", chronicPatientGroupsStatisticsResgister2);
//  	  		//糖尿病规范管理人数
//	  		int chronicPatientGroupsStatisticsGFGL2 = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("糖尿病", codelist.getCodeValue(), year);
//  		    map.put("tnbgfglTotal", chronicPatientGroupsStatisticsGFGL2);
//  		    //冠心病登记人数
//  		    int chronicPatientGroupsStatisticsResgister3 = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"冠心病");
//	  		map.put("gxbdjTotal", chronicPatientGroupsStatisticsResgister3);
//  	  		//冠心病规范管理人数
//	  		int chronicPatientGroupsStatisticsGFGL3 = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("冠心病", codelist.getCodeValue(), year);
//  		    map.put("gxbgfglTotal", chronicPatientGroupsStatisticsGFGL3);
//  		    //肿瘤登记人数
//  		    int chronicPatientGroupsStatisticsResgister4 = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"肿瘤");
//	  		map.put("zldjTotal", chronicPatientGroupsStatisticsResgister4);
//  	  		//肿瘤规范管理人数
//	  		int chronicPatientGroupsStatisticsGFGL4 = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("肿瘤", codelist.getCodeValue(), year);
//  		    map.put("zlgfglTotal", chronicPatientGroupsStatisticsGFGL4);
//  		    //精神病登记人数
//  		    int chronicPatientGroupsStatisticsResgister5 = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"精神病");
//	  		map.put("jsbdjTotal", chronicPatientGroupsStatisticsResgister5);
//  		    //精神病规范管理人数
//	  		int chronicPatientGroupsStatisticsGFGL5 = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("精神病", codelist.getCodeValue(), year);
//  		    map.put("jsbgfglTotal", chronicPatientGroupsStatisticsGFGL5);
//  		    //其它规登记人数(总登记人数-5个慢病人数)
//  		    int chronicPatientGroupsStatisticsResgister6 = reportCardMapper.chronicPatientGroupsStatisticsResgister(codelist.getCodeValue(),year,"");
//  		    map.put("qtdjTotal", (chronicPatientGroupsStatisticsResgister6-chronicPatientGroupsStatisticsResgister5-chronicPatientGroupsStatisticsResgister4-chronicPatientGroupsStatisticsResgister3-chronicPatientGroupsStatisticsResgister2-chronicPatientGroupsStatisticsResgister));
//  		    //其它规范管理人数(总规范管理人数-5个慢病人数)
//  		    int chronicPatientGroupsStatisticsGFGL6 = followupRecordMapper.chronicPatientGroupsStatisticsGFGL("", codelist.getCodeValue(), year);
//  		    map.put("qtgfglTotal", (chronicPatientGroupsStatisticsGFGL6-chronicPatientGroupsStatisticsGFGL5-chronicPatientGroupsStatisticsGFGL4-chronicPatientGroupsStatisticsGFGL3-chronicPatientGroupsStatisticsGFGL2-chronicPatientGroupsStatisticsGFGL));
//  		    list.add(map);
//  		}
//  		return list;
//  	}



  	/**
  	 * 规范人员管理统计表
  	 * @return
  	 */
  	public List<Map<String,Object>> chronicPatientGroupsStatistics(String hosnum,String year){
  		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  		//所有村名
  		List<Codelist> chronicPatientGroupsStatisticsCun = codelistMapper.chronicPatientGroupsStatisticsCun(hosnum);
  		for (Codelist codelist : chronicPatientGroupsStatisticsCun) {
  			Map<String,Object> map= new HashMap<String,Object>();
  			map.put("cunName", codelist.getCodeName());
  			//每个村的总人口数
  			int chronicPatientGroupsStatisticsTotal = mbPopulationMapper.chronicPatientGroupsStatisticsTotal(year, codelist.getCodeValue());
  			map.put("cunTotal", chronicPatientGroupsStatisticsTotal);
  			//确诊人数
  			int chronicPatientGroupsStatisticsQZTotal = mbPopulationMapper.chronicPatientGroupsStatisticsQZTotal(year, codelist.getCodeValue());
  			map.put("cunQZTotal", chronicPatientGroupsStatisticsQZTotal);
  			//慢病 登记人数、规范管理人数
  			Map<String, Object> test1 = reportCardMapper.test1(codelist.getCodeValue(), year);
  			Map<String, Object> test2 = reportCardMapper.test2(codelist.getCodeValue(), year);
  			map.putAll(test2);
  			map.putAll(test1);
  		    list.add(map);
  		}
  		return list;
  	}

  	/**
  	 * 慢病人员药品使用统计患者查询
  	 * @param hosnum
  	 * @param patientName
  	 * @param mbType
  	 * @return
  	 */
  	public List<Map<String,Object>> ywsyPatient(String hosnum,String patientName,String mbType){
  		return reportCardMapper.ywsyPatient(hosnum, patientName, mbType);
  	}

  	/**
  	 * 慢病人员药品使用统计患者信息
  	 * @param bkbh
  	 * @return
  	 */
  	public Map<String,Object> mbryypsybPatientInfo(String bkbh){
  		return reportCardMapper.mbryypsybPatientInfo(bkbh);
  	}

  	/**
  	 * 慢病人员药品使用统计患者药品信息
  	 * @param bkbh
  	 * @return
  	 */
  	public List<Map<String,Object>> mbryypsybPatientMedInfo(String bkbh){
  		return reportCardMapper.mbryypsybPatientMedInfo(bkbh);
  	}

  	/**
  	 * 药物使用情况统计表
  	 * @param hosnum
  	 * @param year
  	 * @return
  	 */
  	public List<Map<String,Object>> ywsyqktjb(String hosnum,String year){
  		return reportCardMapper.ywsyqktjb(hosnum,year);
  	}

  	/**
  	 * 慢病患者健康档案查询
  	 * @param mbReportCard
  	 * @return
  	 */
  	public List<Map<String,Object>> getMbhealthFilesList(MbReportCard mbReportCard){
  		return reportCardMapper.getMbhealthFilesList(mbReportCard);
  	}

  	/**
  	 * 体检人群统计表
  	 * @return
  	 */
  	public List<Map<String,Object>> pexamPopulation(String hosnum,String startDate,String endDate){
  		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
  		//所有村名
  		/*List<Codelist> chronicPatientGroupsStatisticsCun = codelistMapper.chronicPatientGroupsStatisticsCun(hosnum);
  		for (Codelist codelist : chronicPatientGroupsStatisticsCun) {
  			Map<String,Object> map= new HashMap<String,Object>();
  			map.put("cunName", codelist.getCodeName());
  			//按年龄查询数量
  			Map<String, Object> pexamPopulation = reportCardMapper.pexamPopulation(codelist.getCodeValue(), startDate, endDate);
  			map.putAll(pexamPopulation);
  			list.add(map);
  		}*/
  		return reportCardMapper.pexamPopulation(hosnum, startDate, endDate);
  	}
	/**
	 * 体检人群统计表（新）
	 * @return
	 */
	public List<Map<String,Object>> pexamPopulation2(String hosnum,String queryDay){
		//所有村名
  		/*List<Codelist> chronicPatientGroupsStatisticsCun = codelistMapper.chronicPatientGroupsStatisticsCun(hosnum);
  		for (Codelist codelist : chronicPatientGroupsStatisticsCun) {
  			Map<String,Object> map= new HashMap<String,Object>();
  			map.put("cunName", codelist.getCodeName());
  			//按年龄查询数量
  			Map<String, Object> pexamPopulation = reportCardMapper.pexamPopulation(codelist.getCodeValue(), startDate, endDate);
  			map.putAll(pexamPopulation);
  			list.add(map);
  		}*/
		String startDate = queryDay.split("~")[0];
		String endDate = queryDay.split("~")[1];
		List<Map<String, Object>> mbClassificationInfoList = followupRecordMapper.getMbClassificationInfoList();
		StringBuffer sql_mb = new StringBuffer();
		StringBuffer sql_select_mb = new StringBuffer();
		for (Map<String, Object> mbClassificationInfo : mbClassificationInfoList) {
			sql_select_mb.append("IFNULL(b." + mbClassificationInfo.get("mb_code") + ", 0) AS " + mbClassificationInfo.get("mb_code") + ",");
			String[] mbInfoArr = (mbClassificationInfo.get("mb_info") + "").split("\\|");
			StringBuffer mbInfoStr = new StringBuffer();
			for (String mbInfo : mbInfoArr) {
				mbInfoStr.append("or mp.chronic LIKE '%" + mbInfo + "%'");
			}
			if (StringUtils.isNotEmpty(mbInfoStr)){
				mbInfoStr = new StringBuffer(mbInfoStr.substring(2));
			}
			sql_mb.append(" count( DISTINCT " +
					"  CASE " +
					"  WHEN " + mbInfoStr + " THEN " +
					"   r1.customerid " +
					"  ELSE " +
					"  null " +
					"  END " +
					"  ) " + mbClassificationInfo.get("mb_code") + ",");
		}
		List<Map<String, Object>> list = reportCardMapper.pexamPopulation2(hosnum, startDate, endDate,sql_mb.toString(),sql_select_mb.toString());
		// TODO: 此处差一个合计计算
//		int peerNum = 0; //人数
		int xnxg = 0; // 心脑血管
		int gxy = 0; // 高血压
		int tnb = 0; // 糖尿病
		int zl = 0; // 肿瘤
		int jsws = 0; // 精神卫生
		int qt = 0; // 其他
		int nanNum = 0; // 男性
		int nvNum = 0; // 女性
		int agelt50 = 0; // 年龄段 低于50
		int age5060 = 0; // 年龄段 50-60
		int age6170 = 0; // 年龄段 61-70
		int agegt70 = 0; // 年龄段 高于70
		for (Map<String, Object> map : list) {
//			peerNum += Integer.parseInt(map.get("peerNum")+"");
			xnxg += Integer.parseInt(map.get("xnxg")+"");
			gxy += Integer.parseInt(map.get("gxy")+"");
			tnb += Integer.parseInt(map.get("tnb")+"");
			zl += Integer.parseInt(map.get("zl")+"");
			jsws += Integer.parseInt(map.get("jsws")+"");
			qt += Integer.parseInt(map.get("qt")+"");
			nanNum += Integer.parseInt(map.get("nanNum")+"");
			nvNum += Integer.parseInt(map.get("nvNum")+"");
			agelt50 += Integer.parseInt(map.get("agelt50")+"");
			age5060 += Integer.parseInt(map.get("age5060")+"");
			age6170 += Integer.parseInt(map.get("age6170")+"");
			agegt70 += Integer.parseInt(map.get("agegt70")+"");
		}
		Map<String,Object> mapHJ = new HashMap<>();
		mapHJ.put("name","合计");
//		mapHJ.put("peerNum",peerNum);
		mapHJ.put("xnxg",xnxg);
		mapHJ.put("gxy",gxy);
		mapHJ.put("tnb",tnb);
		mapHJ.put("zl",zl);
		mapHJ.put("jsws",jsws);
		mapHJ.put("qt",qt);
		mapHJ.put("nanNum",nanNum);
		mapHJ.put("nvNum",nvNum);
		mapHJ.put("agelt50",agelt50);
		mapHJ.put("age5060",age5060);
		mapHJ.put("age6170",age6170);
		mapHJ.put("agegt70",agegt70);
		list.add(mapHJ);
		return list;
	}

    /**
     * 个人体检频次表
     * @param hosnum
     * @param populationName
     * @return
     */
  	public List<Map<String,Object>> getTjpcName(String hosnum,String populationName){
  		return reportCardMapper.getTjpcName(hosnum,populationName);
  	}

  	/**
  	 * 个人体检频次表
  	 * @param populationId
  	 * @return
  	 */
  	public Map<String,Object> getTjpcfo(String populationId){
  		Map<String,Object> map=new HashMap<String,Object>();
  		Map<String, Object> tjpcPopulationInfo = reportCardMapper.getTjpcPopulationInfo(populationId);
  		List<Map<String, Object>> tjpcPexamInfo = reportCardMapper.getTjpcPexamInfo(populationId);
  		map.put("populationInfo", tjpcPopulationInfo);
  		map.put("pexamInfo", tjpcPexamInfo);
  		return map;
  	}

  	/**
  	 * 风险指数统计
  	 * @param hosnum
  	 * @return
  	 */
  	public List<Codelist> fxzs(String hosnum){
  		return codelistMapper.chronicPatientGroupsStatisticsCun(hosnum);
  	}

  	/**
  	 * 症状指标查询
  	 * @param mbreportCard
  	 * @return
  	 */
  	public IPage<MbReportCard> getZzzb(MbReportCard mbreportCard){
        Page<MbReportCard> page = new Page<>(mbreportCard.getPageIndex(),mbreportCard.getPageSize());
        return reportCardMapper.getZzzb(page,mbreportCard);
    }


  	/**
  	 * 症状指标查询导出
  	 * @param mbreportCard
  	 * @return
  	 */
  	public List<MbReportCard> getZzzbExport(MbReportCard mbreportCard){
        return reportCardMapper.getZzzbExport(mbreportCard);
    }


	public List gecunrenshu(String mbType){
  		return reportCardMapper.gecunrenshu(mbType);
	}

	public List gecunrenshush(String mbType){
  		return reportCardMapper.gecunrenshush(mbType);
	}

	/**
	 * 疾病风险统计
	 * @param hosnum
	 * @return
	 */
	public Map<String,Object> getFxzsList(String hosnum,String startDate,String endDate){
		Map<String,Object> map=new HashMap<>();
		List<Map<String,Object>> fxzs = reportCardMapper.getFxzsList(hosnum,startDate,endDate);
		int xgTotal=0;
		int xtTotal=0;
		int plTotal=0;
		int yiyuTotal=0;
		int yanyTotal=0;
		int wyTotal=0;
		int jzbTotal=0;
		int jzbzTotal=0;
		int xyTotal=0;
		int xzTotal=0;
		int jlTotal=0;
		int smTotal=0;
		int mylTotal=0;
		int wdlbzTotal=0;
		int ngxbzTotal=0;
		int sgnjtTotal=0;
		for (int i=0;i<fxzs.size();i++){
			Map<String,Object> item = fxzs.get(i);
			xgTotal = xgTotal + Integer.parseInt(item.get("xg").toString());
			xtTotal = xtTotal + Integer.parseInt(item.get("xt").toString());
			plTotal = plTotal + Integer.parseInt(item.get("pl").toString());
			yiyuTotal = yiyuTotal + Integer.parseInt(item.get("yiyu").toString());
			yanyTotal = yanyTotal + Integer.parseInt(item.get("yany").toString());
			wyTotal = wyTotal + Integer.parseInt(item.get("wy").toString());
			jzbTotal = jzbTotal + Integer.parseInt(item.get("jzb").toString());
			jzbzTotal = jzbzTotal + Integer.parseInt(item.get("jzbz").toString());
			xyTotal = xyTotal + Integer.parseInt(item.get("xy").toString());
			xzTotal = xzTotal + Integer.parseInt(item.get("xz").toString());
			jlTotal = jlTotal + Integer.parseInt(item.get("jl").toString());
			smTotal = smTotal + Integer.parseInt(item.get("sm").toString());
			mylTotal = mylTotal + Integer.parseInt(item.get("myl").toString());
			wdlbzTotal = wdlbzTotal + Integer.parseInt(item.get("wdlbz").toString());
			ngxbzTotal = ngxbzTotal + Integer.parseInt(item.get("ngxbz").toString());
			sgnjtTotal = sgnjtTotal + Integer.parseInt(item.get("sgnjt").toString());
		}
		map.put("fxzs",fxzs);
		map.put("xgTotal",xgTotal);
		map.put("xtTotal",xtTotal);
		map.put("plTotal",plTotal);
		map.put("yiyuTotal",yiyuTotal);
		map.put("yanyTotal",yanyTotal);
		map.put("wyTotal",wyTotal);
		map.put("jzbTotal",jzbTotal);
		map.put("jzbzTotal",jzbzTotal);
		map.put("xyTotal",xyTotal);
		map.put("xzTotal",xzTotal);
		map.put("jlTotal",jlTotal);
		map.put("smTotal",smTotal);
		map.put("mylTotal",mylTotal);
		map.put("wdlbzTotal",wdlbzTotal);
		map.put("ngxbzTotal",ngxbzTotal);
		map.put("sgnjtTotal",sgnjtTotal);
		return map;
	}

	/**
	 * 疾病风险查询
	 * @param jbfxReport
	 * @return
	 */
	public IPage<Map<String,Object>> getJbfxList(JbfxReport jbfxReport){
		Page<JbfxReport> page = new Page<>(jbfxReport.getPageIndex(),jbfxReport.getPageSize());
		return reportCardMapper.getJbfxList(page,jbfxReport);
	}

	/**
	 * 导出疾病风险
	 * @param jbfxReport
	 * @return
	 */
	public List<Map<String,Object>> exportJbfxList(JbfxReport jbfxReport){
		return reportCardMapper.exportJbfxList(jbfxReport);
	}

	/**
	 * 脏腑辩证查询
	 * @param hosnum
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public List<Map<String,Object>> zfbzList(String hosnum,String startDate,String endDate){
		return reportCardMapper.zfbzList(hosnum,startDate,endDate);
	}

	/**
	 * 体质辩证查询
	 * @param hosnum
	 * @return
	 */
	public List<Map<String,Object>> getTzbzList(String hosnum,String startDate,String endDate){
		return reportCardMapper.getTzbzList(hosnum,startDate,endDate);
	}
	/**
	 * 体质辩证查询(新)
	 * @param hosnum
	 * @return
	 */
	public List<Map<String,Object>> getTzbzList2(String hosnum,String queryDay){
		String startDate = queryDay.split("~")[0];
		String endDate = queryDay.split("~")[1];
		return reportCardMapper.getTzbzList2(hosnum,startDate,endDate);
	}

	/**
	 * 体质查询
	 * @param hosnum
	 * @param startDate
	 * @param endDate
	 * @param pname
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	public IPage<Map<String,Object>> getTzList(String hosnum,String startDate,String endDate,String pname,int pageIndex,int pageSize){
		Page<Map<String,Object>> page = new Page<>(pageIndex,pageSize);
		return reportCardMapper.getTzList(page,hosnum,startDate,endDate,pname);
	}

}
